package com.aiyige.base.db.table;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.aiyige.model.moment.backup.TagBackup;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.interest.model.Interest;
import com.aiyige.page.interest.util.InterestUtil;
import com.aiyige.page.publish.selectinterest.model.PublishIndustryItem;
import com.aiyige.page.publish.selectinterest.model.PublishInterestItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.setup.util.SetupUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.MomentUtil;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.stmt.query.In;
import com.j256.ormlite.table.DatabaseTable;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by usera on 2017/11/19.
 */
@DatabaseTable(tableName = "InfoModel")
public class InfoModel implements Parcelable, MultiItemEntity {
    public static final String ID = "id";
    public static final String CONTROL_OPTION = "controlOption";
    public static final String PROGRESS_STATUS = "progressStatus";
    public static final String USER_ID = "userId";
    public static final String GOODS_ID = "momentId";
    public static final String ROUTER = "router";
    public static final String RETRY_COUNT = "retryCount";
    public static final String ARTICLE_JSON = "articleJson";
    public static final String RICH_TEXT = "richText";
    @DatabaseField(id = true, columnName = ID)
    String id;
    @DatabaseField(columnName = USER_ID)
    String userId;
    @DatabaseField
    String title;
    @DatabaseField
    String coverStoreKey;
    @DatabaseField
    String coverUrl;
    @DatabaseField
    String interest;
    @DatabaseField
    String industry;
    @DatabaseField
    String introduction;
    @DatabaseField(columnName = PROGRESS_STATUS)
    int progressStatus;
    @DatabaseField(columnName = CONTROL_OPTION)
    int controlOption;
    @DatabaseField(columnName = GOODS_ID)
    String goodsId;
    @DatabaseField
    String location;
    @DatabaseField
    long releaseDate;
    @DatabaseField(columnName = ARTICLE_JSON)
    String articleJson;
    @DatabaseField(columnName = RICH_TEXT, dataType = DataType.LONG_STRING)
    String richText;
    @DatabaseField(columnName = ROUTER)
    String router;
    @DatabaseField(columnName = RETRY_COUNT)
    int retryCount;
    @DatabaseField
    long sellDate;

    @DatabaseField
    boolean coverNeedUpload = false;
    @DatabaseField
    int publishOperationType = 0;

    float uploadedPercent = 0f;
    String rejectReason;

    long viewCount;
    long likeCount;
    long commentCount;
    long collectCount;

    int publishSource = PublishUtil.PUBLISH_SOURCE_ANDROID;


    public InfoModel() {
    }

    private InfoModel(Builder builder) {
        setId(builder.id);
        setUserId(builder.userId);
        setTitle(builder.title);
        setCoverStoreKey(builder.coverStoreKey);
        setCoverUrl(builder.coverUrl);
        setInterest(builder.interest);
        setIndustry(builder.industry);
        setIntroduction(builder.introduction);
        setProgressStatus(builder.progressStatus);
        setControlOption(builder.controlOption);
        setGoodsId(builder.goodsId);
        setLocation(builder.location);
        setReleaseDate(builder.releaseDate);
        setArticleJson(builder.articleJson);
        setRichText(builder.richText);
        setRouter(builder.router);
        setRetryCount(builder.retryCount);
        setSellDate(builder.sellDate);
        setCoverNeedUpload(builder.coverNeedUpload);
        setPublishOperationType(builder.publishOperationType);
        setUploadedPercent(builder.uploadedPercent);
        setRejectReason(builder.rejectReason);
        setViewCount(builder.viewCount);
        setLikeCount(builder.likeCount);
        setCommentCount(builder.commentCount);
        setCollectCount(builder.collectCount);
        setPublishSource(builder.publishSource);
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    @Override
    public int getItemType() {
        return PublishUtil.PARENT_TYPE_INFO;
    }


    public static InfoModel parse(Moment moment) {
        int progressStatus = PublishUtil.convertMomentStatusToProgressStatus(moment.getStatus());
        List<Interest> industryList = new LinkedList<>();
        List<Interest> interestList = new LinkedList<>();
        for (TagBackup tagBackup : moment.getTags()) {
            if (tagBackup == null || TextUtils.isEmpty(tagBackup.getId())) continue;
            switch (tagBackup.getGroupType()) {
                case SetupUtil.GROUP_TYPE_AVOCATION:
                    interestList.add(InterestUtil.convertTagBackupToInterest(tagBackup));
                    break;
                case SetupUtil.GROUP_TYPE_CHANNEL:
                    industryList.add(InterestUtil.convertTagBackupToInterest(tagBackup));
                    break;
            }
        }
        return InfoModel.newBuilder()
                .id("")
                .userId(AccountUtil.getCurrentUser().getId())
                .title(moment.getTitle())
                .interest(JSON.toJSONString(interestList))
                .industry(JSON.toJSONString(industryList))
                .coverUrl(ListUtil.isEmpty(moment.getCover()) ? "" : moment.getCover().get(0))
                .coverNeedUpload(false)
                .progressStatus(progressStatus)
                .publishSource(MomentUtil.convertAiyigeApplicationToPublishSource(moment.getAiyigeApplication()))
                .goodsId(moment.getId())
                .releaseDate(moment.getUpdateTime())
                .sellDate(moment.getOnsellTime())
                .router(moment.getRouter())
                .rejectReason(moment.getRemark())
                .likeCount(moment.getStatisticsBackup().getPraiseCount())
                .collectCount(moment.getStatisticsBackup().getFavoriteCount())
                .commentCount(moment.getStatisticsBackup().getCommentCount())
                .viewCount(moment.getStatisticsBackup().getViewCount())
                .build();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCoverStoreKey() {
        return coverStoreKey;
    }

    public void setCoverStoreKey(String coverStoreKey) {
        this.coverStoreKey = coverStoreKey;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public String getInterest() {
        return interest;
    }

    public void setInterest(String interest) {
        this.interest = interest;
    }

    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public int getProgressStatus() {
        return progressStatus;
    }

    public void setProgressStatus(int progressStatus) {
        this.progressStatus = progressStatus;
    }

    public int getControlOption() {
        return controlOption;
    }

    public void setControlOption(int controlOption) {
        this.controlOption = controlOption;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public long getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(long releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getArticleJson() {
        return articleJson;
    }

    public void setArticleJson(String articleJson) {
        this.articleJson = articleJson;
    }

    public String getRichText() {
        return richText;
    }

    public void setRichText(String richText) {
        this.richText = richText;
    }

    public String getRouter() {
        return router;
    }

    public void setRouter(String router) {
        this.router = router;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public long getSellDate() {
        return sellDate;
    }

    public void setSellDate(long sellDate) {
        this.sellDate = sellDate;
    }

    public boolean isCoverNeedUpload() {
        return coverNeedUpload;
    }

    public void setCoverNeedUpload(boolean coverNeedUpload) {
        this.coverNeedUpload = coverNeedUpload;
    }

    public int getPublishOperationType() {
        return publishOperationType;
    }

    public void setPublishOperationType(int publishOperationType) {
        this.publishOperationType = publishOperationType;
    }

    public float getUploadedPercent() {
        return uploadedPercent;
    }

    public void setUploadedPercent(float uploadedPercent) {
        this.uploadedPercent = uploadedPercent;
    }

    public String getRejectReason() {
        return rejectReason;
    }

    public void setRejectReason(String rejectReason) {
        this.rejectReason = rejectReason;
    }

    public long getViewCount() {
        return viewCount;
    }

    public void setViewCount(long viewCount) {
        this.viewCount = viewCount;
    }

    public long getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(long likeCount) {
        this.likeCount = likeCount;
    }

    public long getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(long commentCount) {
        this.commentCount = commentCount;
    }

    public long getCollectCount() {
        return collectCount;
    }

    public void setCollectCount(long collectCount) {
        this.collectCount = collectCount;
    }

    public int getPublishSource() {
        return publishSource;
    }

    public void setPublishSource(int publishSource) {
        this.publishSource = publishSource;
    }

    public static final class Builder {
        private String id;
        private String userId;
        private String title;
        private String coverStoreKey;
        private String coverUrl;
        private String interest;
        private String industry;
        private String introduction;
        private int progressStatus;
        private int controlOption;
        private String goodsId;
        private String location;
        private long releaseDate;
        private String articleJson;
        private String richText;
        private String router;
        private int retryCount;
        private long sellDate;
        private boolean coverNeedUpload;
        private int publishOperationType;
        private float uploadedPercent;
        private String rejectReason;
        private long viewCount;
        private long likeCount;
        private long commentCount;
        private long collectCount;
        private int publishSource;

        private Builder() {
            id = "";
            userId = "";
            title = "";
            coverStoreKey = "";
            coverUrl = "";
            interest = "";
            industry = "";
            introduction = "";
            goodsId = "";
            location = "";
            router = "";
            articleJson = "";
            coverNeedUpload = true;
            publishOperationType = PublishUtil.PUBLISH_OPERATION_TYPE_SUBMIT_REVIEW;
            viewCount = 0;
            likeCount = 0;
            commentCount = 0;
            collectCount = 0;
            rejectReason = "";
            richText = "";
            publishSource = PublishUtil.PUBLISH_SOURCE_ANDROID;
            sellDate = 0;
        }

        public Builder id(String val) {
            id = val;
            return this;
        }

        public Builder userId(String val) {
            userId = val;
            return this;
        }

        public Builder title(String val) {
            title = val;
            return this;
        }

        public Builder coverStoreKey(String val) {
            coverStoreKey = val;
            return this;
        }

        public Builder coverUrl(String val) {
            coverUrl = val;
            return this;
        }

        public Builder interest(String val) {
            interest = val;
            return this;
        }

        public Builder industry(String val) {
            industry = val;
            return this;
        }

        public Builder introduction(String val) {
            introduction = val;
            return this;
        }

        public Builder progressStatus(int val) {
            progressStatus = val;
            return this;
        }

        public Builder controlOption(int val) {
            controlOption = val;
            return this;
        }

        public Builder goodsId(String val) {
            goodsId = val;
            return this;
        }

        public Builder location(String val) {
            location = val;
            return this;
        }

        public Builder releaseDate(long val) {
            releaseDate = val;
            return this;
        }

        public Builder articleJson(String val) {
            articleJson = val;
            return this;
        }

        public Builder richText(String val) {
            richText = val;
            return this;
        }

        public Builder router(String val) {
            router = val;
            return this;
        }

        public Builder retryCount(int val) {
            retryCount = val;
            return this;
        }

        public Builder sellDate(long val) {
            sellDate = val;
            return this;
        }

        public Builder coverNeedUpload(boolean val) {
            coverNeedUpload = val;
            return this;
        }

        public Builder publishOperationType(int val) {
            publishOperationType = val;
            return this;
        }

        public Builder uploadedPercent(float val) {
            uploadedPercent = val;
            return this;
        }

        public Builder rejectReason(String val) {
            rejectReason = val;
            return this;
        }

        public Builder viewCount(long val) {
            viewCount = val;
            return this;
        }

        public Builder likeCount(long val) {
            likeCount = val;
            return this;
        }

        public Builder commentCount(long val) {
            commentCount = val;
            return this;
        }

        public Builder collectCount(long val) {
            collectCount = val;
            return this;
        }

        public Builder publishSource(int val) {
            publishSource = val;
            return this;
        }

        public InfoModel build() {
            return new InfoModel(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.userId);
        dest.writeString(this.title);
        dest.writeString(this.coverStoreKey);
        dest.writeString(this.coverUrl);
        dest.writeString(this.interest);
        dest.writeString(this.industry);
        dest.writeString(this.introduction);
        dest.writeInt(this.progressStatus);
        dest.writeInt(this.controlOption);
        dest.writeString(this.goodsId);
        dest.writeString(this.location);
        dest.writeLong(this.releaseDate);
        dest.writeString(this.articleJson);
        dest.writeString(this.richText);
        dest.writeString(this.router);
        dest.writeInt(this.retryCount);
        dest.writeLong(this.sellDate);
        dest.writeByte(this.coverNeedUpload ? (byte) 1 : (byte) 0);
        dest.writeInt(this.publishOperationType);
        dest.writeFloat(this.uploadedPercent);
        dest.writeString(this.rejectReason);
        dest.writeLong(this.viewCount);
        dest.writeLong(this.likeCount);
        dest.writeLong(this.commentCount);
        dest.writeLong(this.collectCount);
        dest.writeInt(this.publishSource);
    }

    protected InfoModel(Parcel in) {
        this.id = in.readString();
        this.userId = in.readString();
        this.title = in.readString();
        this.coverStoreKey = in.readString();
        this.coverUrl = in.readString();
        this.interest = in.readString();
        this.industry = in.readString();
        this.introduction = in.readString();
        this.progressStatus = in.readInt();
        this.controlOption = in.readInt();
        this.goodsId = in.readString();
        this.location = in.readString();
        this.releaseDate = in.readLong();
        this.articleJson = in.readString();
        this.richText = in.readString();
        this.router = in.readString();
        this.retryCount = in.readInt();
        this.sellDate = in.readLong();
        this.coverNeedUpload = in.readByte() != 0;
        this.publishOperationType = in.readInt();
        this.uploadedPercent = in.readFloat();
        this.rejectReason = in.readString();
        this.viewCount = in.readLong();
        this.likeCount = in.readLong();
        this.commentCount = in.readLong();
        this.collectCount = in.readLong();
        this.publishSource = in.readInt();
    }

    public static final Creator<InfoModel> CREATOR = new Creator<InfoModel>() {
        @Override
        public InfoModel createFromParcel(Parcel source) {
            return new InfoModel(source);
        }

        @Override
        public InfoModel[] newArray(int size) {
            return new InfoModel[size];
        }
    };
}
